package gdt.entity;

import gdt.entity.AbstractSecureEntity;
import java.io.Serializable;
import java.lang.String;
import java.util.Date;

import javax.persistence.*;

import org.hibernate.validator.Length;
import org.hibernate.validator.NotNull;

import static javax.persistence.FetchType.LAZY;

/**
 * Entity that represents a task within a project category.
 */

@NamedQueries({
@NamedQuery(name="findTasksByCategoryAndUser",query="select t from Task t where t.category = :category and t.assignedMember = :member")
})
@Entity
public class Task extends AbstractSecureEntity implements Serializable {

	private static final long serialVersionUID = 1L;
	
	@NotNull
	private Date startDate;
	
	@NotNull
	private Date endDate;
	
	@Length(max=255)
	private String comment;
	
	@ManyToOne(fetch=LAZY)
	private Category category;
	
	@ManyToOne(fetch=LAZY)
	private Member assignedMember;

	
	public Task() {
		super();
	}   
	
	// ********************** Accessor Methods ********************** //
	 
	public Date getStartDate() {
		return this.startDate;
	}

	public void setStartDate(Date startDate) {
		this.startDate = startDate;
	}   
	public Date getEndDate() {
		return this.endDate;
	}

	public void setEndDate(Date endDate) {
		this.endDate = endDate;
	}   
	public String getComment() {
		return this.comment;
	}

	public void setComment(String comment) {
		this.comment = comment;
	}
	public void setCategory(Category category) {
		this.category = category;
	}
	public Category getCategory() {
		return category;
	}

	public Member getAssignedMember() {
		return assignedMember;
	}

	public void setAssignedMember(Member assignedMember) {
		this.assignedMember = assignedMember;
	}
	
	public long getDuration(){
		if(startDate != null && endDate != null)
			return endDate.compareTo(startDate);
		else
			return 0;
	}
	
	public Project getProject(){
		Category c = getCategory();
		while(c.getParentProject() == null){
			c = c.getParent();
		}
		return c.getParentProject();
	}
   
}
